<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;
use think\Db;
use think\Model;
use think\Request;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 列表页
     *
     * @ApiTitle    (列表页)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/index)
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams   (name="page", type="integer", required=true, description="页码")
     * @ApiParams   (name="limit", type="integer", required=false, description="每页页数")
     * @ApiParams   (name="order", type="string", required=false, description="排序方式")
     * @ApiParams   (name="id", type="string", required=false, description="爱情ID")
     * @ApiParams   (name="gender", type="integer", required=false, description="性别")
     * @ApiParams   (name="income", type="integer", required=false, description="年薪")
     * @ApiParams   (name="degree", type="integer", required=false, description="学历")
     * @ApiParams   (name="ismarr", type="integer", required=false, description="婚姻")
     * @ApiParams   (name="zodiac", type="integer", required=false, description="生肖")
     * @ApiParams   (name="constellation", type="integer", required=false, description="星座")
     * @ApiParams   (name="age", type="integer", required=false, description="年龄")
     * @ApiReturn   ({
    'code':'200',
    'msg':'返回成功'
    })
     */
    public function index()
    {
        $val = $_POST;
        $page=intval($this->request->param('page'))?:1;
        $limit=intval($this->request->param('limit'))?:10;
        $where['status'] = 'normal';
        $where['examined']=1;
        //ID号
        if ($love_id=$this->request->param('id')) {
            $where['love_id'] = $love_id;
        }
        if ($gender=$this->request->param('gender')) {
            $where['gender'] = $gender;
        }
        //年薪
        if ($income=$this->request->param('income')) {
            $where['income'] = $income;
        }
        //学历
        if ($degree=$this->request->param('degree')) {
            $where['education'] = $degree;
        }
        //婚姻
        if ($marry=$this->request->param('ismarr')) {
            $where['marry'] = $marry;
        }
        //生肖
        if ($zodiac=$this->request->param('zodiac')) {
            $where['zodiac'] = $zodiac;
        }
        //星座
        if ($constellation=$this->request->param('constellation')) {
            $where['constellation'] = $constellation;
        }
        //年龄
        if ($age=$this->request->param('age')) {
            if ($age == 1) {
                $where['age'] = ['lt', 24];
            }
            if ($age == 2) {
                $where['age'] = ['between', [24, 26]];
            }
            if ($age == 3) {
                $where['age'] = ['between', [27, 28]];
            }
            if ($age == 4) {
                $where['age'] = ['between', [29, 30]];
            }
            if ($age == 5) {
                $where['age'] = ['between', [31, 33]];
            }
            if ($age == 6) {
                $where['age'] = ['between', [34, 35]];
            }
            if ($age == 7) {
                $where['age'] = ['between', [36, 38]];
            }
            if ($age == 8) {
                $where['age'] = ['between', [38, 40]];
            }
            if ($age == 9) {
                $where['age'] = ['between', [40, 48]];
            }
            if ($age == 10) {
                $where['age'] = ['gt', 48];
            }
        }
        $count = Model('user')->where($where)->count();
        $pages=ceil($count/$limit);
        if ($this->request->param('order')) {
            $data = Model('user')
                ->where($where)
                ->page($page,$limit)
                ->order("id", "desc")
                ->select();
        } else {
            $data = Model('user')
                ->where($where)
                ->page($page,$limit)
                ->order("love_id","desc")
                ->select();
        }
        foreach ($data as $key => $v) {
            $data[$key]['year'] = howOld(strtotime($v['birthday'])) + 1;
            $data[$key]['education'] = Config::get('info.degree')[$v['education']];
            $data[$key]['zodiac'] = Config::get('info.animal_sign')[$v['zodiac']];//生肖
        }
        $this->success('ok',['pageInfo'=>['pages'=>$pages,'limit'=>$limit],'info'=>$data],200);
    }

    /**
     * 个人信息
     *
     * @ApiTitle    (资料详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/info)
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiReturn   ({
    'code':'200',
    'msg':'返回成功'
    })
     */
    public function info()
    {
        $id=(int)$this->request->param('id');
        if (!max($id,0)){
            $this->error('id错误','',-1);
        }
        $where=['id'=>$id,'status'=>'normal'];
        $data=Model('user')->where($where)->find();
        if (!$data){
            $this->error('暂无资料','',-2);
        }
        $data['year'] = howOld(strtotime($data['birthday']));
        $data['education_str'] = Config::get('info.degree')[$data['education']];
        $data['zodiac_str'] = Config::get('info.animal_sign')[$data['zodiac']];//生肖
        $data['income_str'] = Config::get('info.income')[$data['income']];
        //奖&&分割  去除第一个空数组  会做的菜
        if ($data['cook_list'] && is_array($data['cook_list'])){
            $cook_lists='';
            $lists = array_filter(explode('&&', $data['cook_list']));
            foreach ($lists as $value) {
                $cook_lists .= $value . ',';
            }
            $data['cook_list']=$cook_lists;
        }

        if ($data['gender']==1){
            $data['gender_str']='男';
        }else{
            $data['gender_str']='女';
        }
        if($data['parents']==1){
            $data['parents_str']='健在';
        }else{
            $data['parents_str']='否';
        }
        if ($data['marry']==1){
            $data['marry_str']='离异';
        }elseif($data['marry']==2){
            $data['marry_str']='丧偶';
        }elseif($data['marry']==3){
            $data['marry_str']='短婚未育';
        }else{
            $data['marry_str']='未婚';
        }

        Model('user')->where($where)->setInc('page_view');//增加访问量
        $this->success('ok',['data'=>$data],200);
    }
    /**
     * 活动列表
     *
     * @ApiTitle    (活动列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/active)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="page", type="integer", required=true, description="页码")
     * @ApiParams   (name="limit", type="integer", required=false, description="每页页数")
     * @ApiReturn   ({
    'code':'200',
    'msg':'返回成功'
    })
     */
    public function active(){
        $page=intval($this->request->param('page'))?:1;
        $limit=intval($this->request->param('limit'))?:10;
        $where['status'] = 'normal';
        $list=Model('active')->where($where)->page($page,$limit)->select();
        if ($list){
            $this->success('OK',['data'=>$list],200);
        }
        $this->error('false','',-1);
    }

    /**
     * 首页banner
     *
     * @ApiTitle    (banner)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/ads)
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     * @ApiReturn   ({
    'code':'200',
    'msg':'返回成功'
    })
     */
    public function ads(){
        $list=Model('Ads')->where(['status'=>1])->where('starttime','<=',time())->where('endtime','>=',time())->order('sort','desc')->select();
        if ($list){
            $this->success('ok',['data'=>$list],200);
        }
        $this->error('false','',-1);
    }
}
